package designMode.statePattern.after;

/**
 * @author LIBOFEI
 * @ClassName GumballMachine
 * @description 糖果机
 * @date 2020/6/28 15:29
 * @Version 1.0
 **/
public class GumballMachine {

   State soldOutState;
   State noQuarterState;
   State hasQuarterState;
   State soldState;
   State winnerState;
    /** *
     * 一开始被设置为售完
     * @author LIBOFEI
     * @date 15:31 2020/6/28
     * @Version  1.0
     **/
   State state = soldOutState;

    /** *
     * 糖果数量
    **/
    int count;

    public GumballMachine(int count){
        this.soldOutState = new SoldOutState(this);
        this.noQuarterState = new NoQuarterState(this);
        this.hasQuarterState = new HasQuarterState(this);
        this.soldState = new SoldState(this);
        this.winnerState = new WinnerState(this);
        if(count > 0){
            state = noQuarterState;
        }
    }

    /** *
     * 投币操作
     * @author LIBOFEI
     * @date 15:32 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void insertQuarter(){
        state.insertQuarter();
    }

    /**
     * 退币操作
     * @author LIBOFEI
     * @date 15:36 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void ejectQuarter(){
        state.ejectQuarter();
    }

    /** *
     * 转动曲柄 取糖果操作
     * @author LIBOFEI
     * @date 15:38 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void turnCrank(){
        state.turnCrank();
    }

    /** *
     * 发放糖果
     * @author LIBOFEI
     * @date 15:41 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void dispense(){
        state.dispense();
    }

    /** *
     * 允许切换对象状态
     * @param state
     * @author LIBOFEI
     * @date 17:05 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void setState(State state) {
        this.state = state;
    }

    public void repleaseBall(){
        System.out.println("赠送糖果");
        if(count != 0){
            count--;
        }
    }

    public void reFill(int count){
        this.count += count;
    }

    public int getCount() {
        return count;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getState() {
        return state;
    }

    public State getWinnerState() {
        return winnerState;
    }


}
